package org.jugile.restapi;

import java.util.HashMap;
import java.util.Map;

import org.jugile.restapi.domain.Operation;
import org.jugile.util.Jugile;
import org.jugile.util.Props;

public class Match {

	public Match(String path[], String method, String type) {
		this.path = path;
		this.method = method;
		this.type = type;
	}
	
	private final String method;
	public String getMethod() { return method; }
	
	private final String type;
	public String getType() { return type; }
	
	private final String path[];
	//public void setPath(String p[]) { path = p; }
	public String[] getPath() { return path; }
	public String getPath(int idx) {
		if (idx > path.length-1) return null;
		return path[idx];
	}
	public boolean isNotLast(int idx) {
		if (isStopped) return false;
		return idx < path.length-1;
	}
	private boolean isStopped = false;
	public void setIsStopped() { isStopped = true; }
	public boolean getIsStopped() { return isStopped; }

	public void setRest(String name, int idx) {
		String value = "";
		for (int i = idx; i < path.length; i++) {
			if (i != idx) value += "/";
			value += path[i];
		}
		set(name,value);
	}
	
	private String lang;
	public String getLang() { 
		if (lang == null) return "fi";
		return lang;
	} 
	public void setLang(String v) { 
		if (lang == null) lang = v;
	}
	
	private Map<String,String> vars = new HashMap<String,String>();
	public void set(String name, String value) { vars.put(name, value); }
	public void set(String name, int idx) { vars.put(name, path[idx]); }
	public String get(String name) { return vars.get(name); }
	public Map<String,String> getVars() { return vars; }
	
	
	private Map<String,String> parts = new HashMap<String,String>();
	public void setPart(String name, String value) { parts.put(name, value); }
	public String getPart(String name) { return parts.get(name); }


	private Operation op;
	public void setOp(Operation op) { this.op = op; }
	
	public String getControllerClassName() { return op.resolveControllerClassName(); }
	public String getMimetype() { return op.resolveMimetype(); }
	public String getModelFile() { return op.resolveModel(); }
	public String getModelDir() { return op.resolveModelDir(); }
	public String getDataModelFile(String name) { return op.resolveDataModelFile(name); } 
	public String getTmplFile() { return op.resolveTmpl(); }
	public String getTmplDir() { return op.resolveTmplDir(); }
	public String getBorderFile() { return op.resolveBorderFile(); }
	

	private Object app;
	public Object getApp() {
		if (app != null) return app;
		String name = "App";
		String pack = Props.getWebPackage();
		app = Jugile.instantiateObject(pack + "." + name);
		return app;
	}

	private Map<String,String> params;
	public void setParams(Map<String,String> m) { params = m; }
	public Map<String,String> getParams() { return params; }
	
}
